package com.ajk.shop.dao;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.ajk.shop.dao.provider.OrderMapperProvider;
import com.ajk.shop.model.Goods;
import com.ajk.shop.model.OrderDetail;
import com.ajk.shop.model.OrderInfo;
import com.ajk.shop.model.OrderInvoice;
import com.ajk.shop.model.UserInvoice;
import com.ajk.shop.model.request.OrderInfoReq;
import com.ajk.shop.model.response.OrderDetailsRes;
import com.ajk.shop.model.response.OrderInfoRes;


/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2016年1月6日 下午4:52:57 
 * 类说明 
 */
public interface OrderMapper {

	@Insert("insert into order_info (order_no,user_id,order_status,receiver,province,city,district,postcode,address,tel,"
			+ "mobile,goods_number,order_payee,shipping_payee,total_payee,coupon_id,coupon_amount,is_invoice,invoice_id,invoice_amount,"
			+ "confirm_time,pay_status,buyer_memo,expiration_time,order_title,order_img,pay_time,source)"
			+ " value(#{orderNo},#{userId},#{orderStatus},#{receiver},#{province},#{city},#{district},#{postcode},#{address},#{tel},"
			+ "#{mobile},#{goodsNumber},#{orderPayee},#{shippingPayee},#{totalPayee},#{couponId},#{couponAmount},#{isInvoice},#{invoiceId},#{invoiceAmount},"
			+ "current_timestamp(),${payStatus},#{buyerMemo},#{expirationTime},#{orderTitle},#{orderImg},#{payTime},#{source})")
	@Options(useGeneratedKeys=true)
	public Integer createOrderInfo(OrderInfo orderInfo);
	
	
	@Insert("insert into order_detail(order_id,goods_id,goods_name,goods_batch_number,sale_price,real_price,goods_number,product_id) "
			+ " value(#{orderId},#{goodsId},#{goodsName},#{goodsBatchNumber},#{salePrice},#{realPrice},#{goodsNumber},#{productId})")
	public Integer createOrderDetail(OrderDetail orderDetail);
	
	@InsertProvider(type=OrderMapperProvider.class,method="batchCreateOrderDetail")
	public Integer batchCreateOrderDetail(@Param("orderDetails") List<OrderDetail> orderDetails,@Param("orderId") Integer orderId);
	
	@Insert("insert into user_invoice(user_id,title) value(#{userId},#{title})")
	@Options(useGeneratedKeys=true)
	public Integer createUserInvoice(UserInvoice userInvoice);
	
	@Insert("insert into order_invoice(order_id,title,amount,create_time)"
			+ " value(#{orderId},#{title},#{amount},current_timestamp())")
	public Integer createOrderInvoice(OrderInvoice orderInvoice);
	
	@Update("update order_info set order_status = #{orderStatus},cancel_time = current_timestamp() where id = #{orderId}")
	public Integer cancelOrderInfo(@Param("orderId") Integer orderId,@Param("orderStatus") Integer orderStatus);
	
	@Update("update order_info set order_status = #{orderStatus},shipping_finish_time = current_timestamp(),"
			+ "shipping_status = #{shippingStatus} where id = #{orderId}")
	public Integer confirmReceipt(@Param("orderId") Integer orderId,@Param("orderStatus") Integer orderStatus,
			@Param("shippingStatus") Integer shippingStatus);
	/**
	 * 订单支付成功--如果是实体商品
	 * @param orderNo
	 * @param orderStatus
	 * @param payChannel
	 * @param payStatus
	 * @return
	 */
	@Update("update order_info set pay_channel = #{payChannel},pay_status = #{payStatus},"
			+ " pay_time = current_timestamp(),ch_id = #{chId},pay_channel = #{payChannel} where order_no = #{orderNo}" )
	public Integer updateOrderPaySuccessForReal(@Param("orderNo") String orderNo,
			@Param("payChannel") String payChannel,
			@Param("payStatus") Integer payStatus,
			@Param("chId") String chId);
	
	/**
	 * 订单支付成功--如果没有实体商品
	 * @param orderNo
	 * @param orderStatus
	 * @param payChannel
	 * @param payStatus
	 * @return
	 */
	@Update("update order_info set pay_channel = #{payChannel},pay_status = #{payStatus},"
			+ " pay_time = current_timestamp(),ch_id = #{chId},order_status = #{orderStatus},pay_channel = #{payChannel}"
			+ " where order_no = #{orderNo}" )
	public Integer updateOrderPaySuccessForService(@Param("orderNo") String orderNo,
			@Param("payChannel") String payChannel,
			@Param("payStatus") Integer payStatus,
			@Param("orderStatus") Integer orderStatus,
			@Param("chId") String chId);
	
	
	
	public String ORDER_INFO_COLUMN = "a.id,a.order_no,a.parent_order_id,a.user_id,a.receiver,a.country,a.order_img,"
			+ "a.province,a.city,a.district,a.postcode,a.address,a.tel,a.mobile,a.goods_number,a.goods_weight,a.order_payee,"
			+ "a.shipping_payee,a.total_payee,a.coupon_id,a.coupon_amount,a.is_invoice,a.invoice_id,a.invoice_amount,"
			+ "a.create_time,a.confirm_time,a.pay_channel,a.pay_status,a.pay_time,a.shipping_channel,a.shiipping_sn,"
			+ "a.shipping_status,a.shipping_time,a.shipping_finish_time,a.buyer_memo,a.cancel_time,a.expiration_time,a.order_title";
	
	@Select("select * from goods where id = #{goodsId}")
	public Goods getGoodsById(Integer goodsId);
	
	@Select("select * from user_invoice where title = #{title}")
	public UserInvoice getUserInvoiceByTitle(String title);
	
	@SelectProvider(type=OrderMapperProvider.class,method="userOrderList")
	public List<OrderInfoRes> userOrderLisr(OrderInfoReq orderInfoReq);
	
	@SelectProvider(type=OrderMapperProvider.class,method="orderList")
	public List<OrderInfoRes> orderLisr(OrderInfoReq orderInfoReq);
	
	@SelectProvider(type=OrderMapperProvider.class,method="orderListCount")
	public Integer orderLisrCount(OrderInfoReq orderInfoReq);
	
	/**
	 * 根据ID获取订单信息
	 * @param id
	 * @return
	 */
	@Select("select "+ORDER_INFO_COLUMN+",d.order_status_view order_status,b.name shipping_channel_name,e.title invoice_title from order_info a left join order_status_config d "
			+ " on a.order_status = d.order_status and a.pay_status = d.pay_status and a.shipping_status = d.shipping_status left join "
			+ " shipping_channels b on a.shipping_channel = b.id left join user_invoice e on a.invoice_id = e.id where a.id = #{id}")
	public OrderInfoRes getOrderById(Integer id);
	
	/**
	 * 根据订单ID获取详查 详情
	 * @param orderId
	 * @return
	 */
	@Select("select "+ORDER_INFO_COLUMN+",a.order_status from order_info a where a.id = #{orderId}")
	public OrderInfo getOrderInfoByOrderId(Integer orderId);
	
	/**
	 * 根据ID获取订单信息
	 * @param id
	 * @return
	 */
	@Select("select "+ORDER_INFO_COLUMN+",d.order_status_view order_status from order_info a left join order_status_config d on a.order_status = d.order_status "
			+ " and a.pay_status = d.pay_status and a.shipping_status = d.shipping_status where a.order_no = #{orderNo}")
	public OrderInfoRes getOrderByOrderNo(String orderNo);
	
	@Select("select * from user_invoice where id = (select max(id) from user_invoice where user_id = #{userId})")
	public UserInvoice getUserInvoiceDefault(Integer userId);
	
	@Select("select * from order_detail where order_id = #{orderId}")
	public List<OrderDetailsRes> getOrderDetailList(Integer orderId);
	
	
	/**
	 * 订单支付成功--如果没有实体商品
	/**
	 * Integer id = orderInfoReq.getId();
		Integer shippingChannel = orderInfoReq.getShippingChannel();
		String shiippingSn = orderInfoReq.getShiippingSn();
		Integer shippingStatus = 1; //0 未发货 1.已发货 2.已收货
		Date shippingTime = new Date();
	 * @param orderInfoReq
	 * @return
	 */
	@Update("update order_info set shipping_channel = #{shippingChannel},shiipping_sn=#{shiippingSn},shipping_status=#{shippingStatus},shipping_time=#{shippingTime} where id = #{id}")
	public Integer updateOrderShipping(@Param("id") Integer id,
			@Param("shippingChannel") Integer shippingChannel,
			@Param("shiippingSn") String shiippingSn,
			@Param("shippingStatus") Integer shippingStatus,
			@Param("shippingTime") Date shippingTime);
}
